import os
import random
import time

import pygame
import sys

from game.snake.entity.block import Block
from game.snake.utils.block_action import BlockAction
from game.snake.utils.utils import Utils

pygame.init()
screen = pygame.display.set_mode((800, 600))

# 移动步长全局变量
step = 50
speed = 1
# 设置背景颜色为淡蓝色
bg_color = (255, 255, 255)
screen.fill(bg_color)
# 屏幕大小
width_screen = 20
height_screen = 15
screen = pygame.display.set_mode((step*width_screen, step*height_screen))

pygame.display.flip()
# 方块图片
block_img_bee_path = "bee.png"
block_img_flower_path = "flower.png"

# 文件路径
main_dir = os.path.split(os.path.abspath(__file__))[0]
block_img_bee = Utils.load_image(block_img_bee_path,(step,step),main_dir)
block_img_flower = Utils.load_image(block_img_flower_path,(step,step),main_dir)


def newBlock(type):
    block_new_new = Block(0,0,step,step,(255,255,255))
    block_new_new.x = abs(step * (random.randint(1, width_screen-1)))
    block_new_new.y = abs(step * (random.randint(1, height_screen-1)))
    block_new_new.x = abs(block_new_new.x)
    block_new_new.y = abs(block_new_new.y)
    block_new_new.color = (random.randint(0, 255),random.randint(0, 255),random.randint(0, 255))
    block_new_new.scour = 0;
    block_new_new.time = time.time();
    #获取随机数字+字母
    # block_new_new.setText(pygame,str(chr(random_num)))
    # block_img_new = Utils.load_image(block_img_flower_path,block_new_new.size(),main_dir)
    # 生成随机整数
    block_new_new.text = type+str(random.randint(0,999999));
    if type == "bee":
        block_new_new.setImage(block_img_bee)
        block_new_new.setText(pygame,"0")
    else:
        block_new_new.setImage(block_img_flower);
    return block_new_new

def game_loop():
    # 开始时间
    time_start = time.time()
    print(time_start)


    # block_0 = Block(300,300,step,step,(255,255,255))
    # block_0.setText(pygame,"1")
    block_bee_group = [];
    block_bee_group.append(newBlock("bee"))
    block_flow_group = [];
    block_flow_group.append(newBlock("flower"))

    while True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        # 移动方块
        for block_item in block_bee_group:
            BlockAction.moveAuto(BlockAction,block_item,speed,screen)
        # 移动方块
        for block_item in block_flow_group:
            BlockAction.moveAuto(BlockAction,block_item,speed,screen)

        #判断方块bee和flower的碰撞
        result = BlockAction.checkCollision(block_bee_group,block_flow_group)
        if result:
            block_bee_sc = result["block_bee_sc"]
            block_flow_sc = result["block_flow_sc"]
            if len(block_flow_sc)>0 and len(block_bee_sc)>0:
                block_flow_group = [item for item in block_flow_group if item not in  block_flow_sc]
                for block_bee in block_bee_sc:
                    for block in block_bee_group:
                        if block == block_bee:
                            block.is_x_left = not block.is_x_left
                            block.is_y_donw = not block.is_y_donw
                            block.scour = block.scour+1
                            block.setText(pygame,str(block.scour))
                            print("蜜蜂：",block_bee.text,"吃掉了花朵：",block.text)


        print(len(block_bee_group))
        block_bee_remove = []
        time_end_bee = time.time()
        for block_item in block_bee_group:
            print("蜜蜂：",block_item.text,"采取蜂蜜数量：",block_item.scour,"存活时间：",int(time_end_bee-block_item.time))
            if time_end_bee-block_item.time>10 and block_item.scour <1:
                # 超过10s中，还没有吃到花朵的小蜜蜂，将要被移除掉
                block_bee_remove.append(block_item)
            if time_end_bee-block_item.time>20 and block_item.scour <3:
                # 超过20s中，还没有吃到花朵的小蜜蜂，将要被移除掉
                block_bee_remove.append(block_item)
            if block_item.scour>2:
                # 当蜜蜂Score大于2，则增加一个蜜蜂，同时减少自身的Score
                block_item.scour = 2
                block_bee_group.append(newBlock("bee"))

        block_bee_group = [item for item in block_bee_group if item not in  block_bee_remove]
        # print("花朵的数量是：",len(block_flow_group),"内存占用：",psutil.Process(os.getpid()).memory_info().rss/1024/1024,"MB")
        print("蜜蜂的数量是：",len(block_bee_group))
        # 打印当前系统内存
        print()
        print("=======")
        # 如果为空了，则增加一个
        if len(block_flow_group)==0:
            block_flow_group.append(newBlock("flower"))
        if len(block_bee_group)==0:
            block_bee_group.append(newBlock("bee"))
        #设置背景颜色，此处作用，防止方块移动留下残影
        screen.fill(bg_color)
        # 绘制方块
        # BlockAction.drawAll(block_bee_group,1,screen)
        BlockAction.drawAllNoOrder(block_bee_group,screen)
        BlockAction.drawAllNoOrder(block_flow_group,screen)
        # 绘制单个方块
        # block_bee_group[0].draw(screen,pygame)

        # 更新屏幕显示
        pygame.display.update()
        time_now = time.time()
        if time_now-time_start> 3:
            time_start = time_now
            # 新增一个
            block_flow_group.append(newBlock("flower"))

    # 文件路径
if __name__ == '__main__':
    game_loop()
